home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / TURBO PASCAL 1.5 for WIN / OWL.PAK / STDDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-08  |  7.9 KB  |  284 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal for Windows                        }
  5. {       Standard dialogs unit for ObjectWindows         }
  6. {                                                       }
  7. {       Copyright (c) 1991 Borland International        }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit StdDlgs;
  12.  
  13. interface
  14.  
  15. uses WinTypes, WinProcs, WinDos, WObjects, Strings;
  16.  
  17. {$R STDDLGS}
  18.  
  19. const
  20.   sd_FileOpen = $7FFF;          { Use default file open template }
  21.   sd_FileSave = $7FFE;          { Use default file save template }
  22.   sd_WNFileOpen = $7F00;        { Normal file open template }
  23.   sd_WNFileSave = $7F01;        { Nomral file save template }
  24.   sd_BCFileOpen = $7F03;        { BWCC file open template }
  25.   sd_BCFileSave = $7F04;        { BWCC file save template }
  26.  
  27. const
  28.   id_FName = 100;
  29.   id_FPath = 101;
  30.   id_FList = 102;
  31.   id_DList = 103;
  32.  
  33. const
  34.   fsFileSpec = fsFileName + fsExtension;
  35.  
  36. type
  37.   PFileDialog = ^TFileDialog;
  38.   TFileDialog = object(TDialog)
  39.     Caption: PChar;
  40.     FilePath: PChar;
  41.     PathName: array[0..fsPathName] of Char;
  42.     Extension: array[0..fsExtension] of Char;
  43.     FileSpec: array[0..fsFileSpec] of Char;
  44.     constructor Init(AParent: PWindowsObject; AName, AFilePath: PChar);
  45.     function CanClose: Boolean; virtual;
  46.     procedure SetupWindow; virtual;
  47.     procedure HandleFName(var Msg: TMessage); virtual id_First + id_FName;
  48.     procedure HandleFList(var Msg: TMessage); virtual id_First + id_FList;
  49.     procedure HandleDList(var Msg: TMessage); virtual id_First + id_DList;
  50.   private
  51.     procedure SelectFileName;
  52.     procedure UpdateFileName;
  53.     function UpdateListBoxes: Boolean;
  54.   end;
  55.  
  56. const
  57.   sd_WNInputDialog = $7F02;     { Normal input dialog template }
  58.   sd_BCInputDialog = $7F05;     { BWCC input dialog template }
  59.  
  60. const
  61.   id_Prompt = 100;
  62.   id_Input  = 101;
  63.  
  64. type
  65.   PInputDialog = ^TInputDialog;
  66.   TInputDialog = object(TDialog)
  67.     Caption: PChar;
  68.     Prompt: PChar;
  69.     Buffer: PChar;
  70.     BufferSize: Word;
  71.     constructor Init(AParent: PWindowsObject;
  72.       ACaption, APrompt, ABuffer: PChar; ABufferSize: Word);
  73.     function CanClose: Boolean; virtual;
  74.     procedure SetupWindow; virtual;
  75.   end;
  76.  
  77. implementation
  78.  
  79. function GetFileName(FilePath: PChar): PChar;
  80. var
  81.   P: PChar;
  82. begin
  83.   P := StrRScan(FilePath, '\');
  84.   if P = nil then P := StrRScan(FilePath, ':');
  85.   if P = nil then GetFileName := FilePath else GetFileName := P + 1;
  86. end;
  87.  
  88. function GetExtension(FilePath: PChar): PChar;
  89. var
  90.   P: PChar;
  91. begin
  92.   P := StrScan(GetFileName(FilePath), '.');
  93.   if P = nil then GetExtension := StrEnd(FilePath) else GetExtension := P;
  94. end;
  95.  
  96. function HasWildCards(FilePath: PChar): Boolean;
  97. begin
  98.   HasWildCards := (StrScan(FilePath, '*') <> nil) or
  99.     (StrScan(FilePath, '?') <> nil);
  100. end;
  101.  
  102. { TFileDialog }
  103.  
  104. constructor TFileDialog.Init(AParent: PWindowsObject;
  105.   AName, AFilePath: PChar);
  106. begin
  107.  
  108.   { If name is sd_FileOpen then use either sd_BCFileOpen or
  109.     sd_WNFileOpen conditional on BWCCClassNames which is set
  110.     to true if BWCC is used }
  111.  
  112.   if AName = PChar(sd_FileOpen) then
  113.     if BWCCClassNames then AName := PChar(sd_BCFileOpen)
  114.     else AName := PChar(sd_WNFileOpen);
  115.  
  116.   { If name is sd_FileSave then use either sd_BCFileSave or
  117.     sd_WNFileSave conditional on BWCCClassNames which is set
  118.     to true if BWCC is used }
  119.  
  120.   if AName = PChar(sd_FileSave) then
  121.     if BWCCClassNames then AName := PChar(sd_BCFileSave)
  122.     else AName := PChar(sd_WNFileSave);
  123.  
  124.   TDialog.Init(AParent, AName);
  125.   Caption := nil;
  126.   FilePath := AFilePath;
  127. end;
  128.  
  129. function TFileDialog.CanClose: Boolean;
  130. var
  131.   PathLen: Word;
  132. begin
  133.   CanClose := False;
  134.   GetDlgItemText(HWindow, id_FName, PathName, fsPathName + 1);
  135.   FileExpand(PathName, PathName);
  136.   PathLen := StrLen(PathName);
  137.   if (PathName[PathLen - 1] = '\') or HasWildCards(PathName) or
  138.     (GetFocus = GetDlgItem(HWindow, id_DList)) then
  139.   begin
  140.     if PathName[PathLen - 1] = '\' then
  141.       StrLCat(PathName, FileSpec, fsPathName);
  142.     if not UpdateListBoxes then
  143.     begin
  144.       MessageBeep(0);
  145.       SelectFileName;
  146.     end;
  147.     Exit;
  148.   end;
  149.   StrLCat(StrLCat(PathName, '\', fsPathName), FileSpec, fsPathName);
  150.   if UpdateListBoxes then Exit;
  151.   PathName[PathLen] := #0;
  152.   if GetExtension(PathName)[0] = #0 then
  153.     StrLCat(PathName, Extension, fsPathName);
  154.   AnsiLower(StrCopy(FilePath, PathName));
  155.   CanClose := True;
  156. end;
  157.  
  158. procedure TFileDialog.SetupWindow;
  159. begin
  160.   SendDlgItemMessage(HWindow, id_FName, em_LimitText, fsPathName, 0);
  161.   if Caption <> nil then SetWindowText(HWindow, Caption);
  162.   StrLCopy(PathName, FilePath, fsPathName);
  163.   StrLCopy(Extension, GetExtension(PathName), fsExtension);
  164.   if HasWildCards(Extension) then Extension[0] := #0;
  165.   if not UpdateListBoxes then
  166.   begin
  167.     StrCopy(PathName, '*.*');
  168.     UpdateListBoxes;
  169.   end;
  170.   SelectFileName;
  171. end;
  172.  
  173. procedure TFileDialog.HandleFName(var Msg: TMessage);
  174. begin
  175.   if Msg.LParamHi = en_Change then
  176.     EnableWindow(GetDlgItem(HWindow, id_Ok),
  177.       SendMessage(Msg.LParamLo, wm_GetTextLength, 0, 0) <> 0);
  178. end;
  179.  
  180. procedure TFileDialog.HandleFList(var Msg: TMessage);
  181. begin
  182.   case Msg.LParamHi of
  183.     lbn_SelChange, lbn_DblClk:
  184.       begin
  185.         DlgDirSelect(HWindow, PathName, id_FList);
  186.         UpdateFileName;
  187.         if Msg.LParamHi = lbn_DblClk then Ok(Msg);
  188.       end;
  189.     lbn_KillFocus:
  190.       SendMessage(Msg.LParamLo, lb_SetCurSel, Word(-1), 0);
  191.   end;
  192. end;
  193.  
  194. procedure TFileDialog.HandleDList(var Msg: TMessage);
  195. begin
  196.   case Msg.LParamHi of
  197.     lbn_SelChange, lbn_DblClk:
  198.       begin
  199.         DlgDirSelect(HWindow, PathName, id_DList);
  200.         StrCat(PathName, FileSpec);
  201.         if Msg.LParamHi = lbn_DblClk then
  202.           UpdateListBoxes else
  203.           UpdateFileName;
  204.       end;
  205.     lbn_KillFocus:
  206.       SendMessage(Msg.LParamLo, lb_SetCurSel, Word(-1), 0);
  207.   end;
  208. end;
  209.  
  210. procedure TFileDialog.SelectFileName;
  211. begin
  212.   SendDlgItemMessage(HWindow, id_FName, em_SetSel, 0, $7FFF0000);
  213.   SetFocus(GetDlgItem(HWindow, id_FName));
  214. end;
  215.  
  216. procedure TFileDialog.UpdateFileName;
  217. begin
  218.   SetDlgItemText(HWindow, id_FName, AnsiLower(PathName));
  219.   SendDlgItemMessage(HWindow, id_FName, em_SetSel, 0, $7FFF0000);
  220. end;
  221.  
  222. function TFileDialog.UpdateListBoxes: Boolean;
  223. var
  224.   Result: Integer;
  225.   Path: array[0..fsPathName] of Char;
  226. begin
  227.   UpdateListBoxes := False;
  228.   if GetDlgItem(HWindow, id_FList) <> 0 then
  229.   begin
  230.     StrCopy(Path, PathName);
  231.     Result := DlgDirList(HWindow, Path, id_FList, id_FPath, 0);
  232.     if Result <> 0 then DlgDirList(HWindow, '*.*', id_DList, 0, $C010);
  233.   end else
  234.   begin
  235.     StrLCopy(Path, PathName, GetFileName(PathName) - PathName);
  236.     StrLCat(Path, '*.*', fsPathName);
  237.     Result := DlgDirList(HWindow, Path, id_DList, id_FPath, $C010);
  238.   end;
  239.   if Result <> 0 then
  240.   begin
  241.     StrLCopy(FileSpec, GetFileName(PathName), fsFileSpec);
  242.     StrCopy(PathName, FileSpec);
  243.     UpdateFileName;
  244.     UpdateListBoxes := True;
  245.   end;
  246. end;
  247.  
  248. { TInputDialog }
  249.  
  250. constructor TInputDialog.Init(AParent: PWindowsObject;
  251.   ACaption, APrompt, ABuffer: PChar; ABufferSize: Word);
  252. var
  253.   AName: PChar;
  254. begin
  255.   if BWCCClassNames then
  256.     AName := PChar(sd_BCInputDialog)
  257.   else
  258.     AName := PChar(sd_WNInputDialog);
  259.  
  260.   TDialog.Init(AParent, AName);
  261.  
  262.   Caption := ACaption;
  263.   Prompt := APrompt;
  264.   Buffer := ABuffer;
  265.   BufferSize := ABufferSize;
  266. end;
  267.  
  268. function TInputDialog.CanClose: Boolean;
  269. begin
  270.   GetDlgItemText(HWindow, id_Input, Buffer, BufferSize);
  271.   CanClose := True;
  272. end;
  273.  
  274. procedure TInputDialog.SetupWindow;
  275. begin
  276.   TDialog.SetupWindow;
  277.   SetWindowText(HWindow, Caption);
  278.   SetDlgItemText(HWindow, id_Prompt, Prompt);
  279.   SetDlgItemText(HWindow, id_Input, Buffer);
  280.   SendDlgItemMessage(HWindow, id_Input, em_LimitText, BufferSize - 1, 0);
  281. end;
  282.  
  283. end.
  284.